package com.mes.basicdata.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.context.annotation.Lazy;

import com.mes.basicdata.dao.ClampDao;
import com.mes.basicdata.dao.CutterDao;
import com.mes.basicdata.dao.MeasurerDao;
import com.mes.common.domain.BasicModel;

/**
 * <p>
 * 工序资源对象，仅仅是POJO，为了得到cutter和clamp，它里面引用了CutterDAO和ClampDAO
 * </p>
 * 
 * <p>
 * Created on Feb 29, 2008
 * </p>
 * 
 * @author lhy
 * 
 */
/**
 * @time 2017/3/18
 * @author SnowBear
 *
 */
@Entity()
@Table(name = "B_OPERATION_RESOURCE")
@BatchSize(size=10)
@Lazy(value=true)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class OperationResource extends BasicModel implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@GenericGenerator(name = "uuid", strategy = "assigned")
	@Column(name="UNIQUEID",scale=0)
	private String uuid;
	
	// 工序
	@Column(name="OPUID" ,precision=30,scale=0)
	private String operationUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="OPUID",insertable=false,updatable=false)
	private Operation operation;
	
	// 可能是刀、卡、量，就是part_number
	@Column(name="MACHINE_NUMBER" ,length=50)
	private String resourceUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="MACHINE_NUMBER",insertable=false,updatable=false)
	private Part resource;
	
	// 资源的图号
	@Column(name="RESOURCE_DRAWINGID")
	private String drawingId;
	// 资源的名称
	@Column(name="RESOURCE_NAME")
	private String resourceName;
	// 资源的规格
	@Column(name="RESOURCE_DESCRIPTION")
	private String resourceDescription;
	// 资源的类型
	@Column(name="RESOURCE_TYPE")
	private String resourceType;
	// 资源的类型描述
	@Column(name="RESOURCE_TYPE_DESC")
	private String resourceTypeDesc;
	
	// 工序使用到该资源的需求数量
	@Column(name="NUM_OPERATORS",precision=10,scale=0)
	private Integer resourceReqQty;
	
	// 用途
	@Column(name="USE_TYPE")
	private String useType;
	
	// 工艺（现在这个属性基本不维护）
	@Column(name="ROUTING")
	private String routingUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="ROUTING",insertable=false,updatable=false)
	private Routing routing;

	// 是否有效
	@Column(name="EFFECTIVE", precision=2,scale=0)
	private Boolean isEffective;
	
	private Cutter cutter;
	private Clamp clamp;
	private Measurer measurer;
	
//	// 同步使用率
//	@Column(name="SIMULTANEOUS_USAGE",length=50)
//	private String simultaneousUsage;
//	
//	// 考虑替换件周期
//	private String considerAltsDuring;
//	
//	// 卸载时间系数
//	private Integer cooldownTimeFactor;
//	
//	// 最小操作时间系数
//	@Column(name="MIN_OPERATION_TIME_FACTOR",precision=10,scale=0)
//	private Integer minOperationTimeFactor;
//	
//	// 卸载等级
//	@Column(name="OFFLOAD_LEVEL",precision=10,scale=0)
//	private Integer offloadLevel;
//	
//	// 每资源的物料数量
//	@Column(name="PART_QTY_PER_RESOURCE", precision=25,scale=6)
//	private Float partQtyPerResource;
//	
//	// 每资源的物料名称
//	@Column(name="PART_QTY_PER_RESOURCE_UOM",length=50)
//	private String partQtyPerResourceUom;
//	
//	// 主资源名称
//	@Column(name="PRIMARY_P" ,precision=2, scale=0)
//	private String primaryResource;
//	
//	// RAMUP日历名称
//	private String ramupCalendarName;
//	
//	// 运行时间系数
//	@Column(name="RUNTIME_FACTOR" ,precision=2,scale=0)
//	private Integer runtimeFactor;
//	
//	// 准备时间系数
//	@Column(name="PREOP_TIME_FACTOR", precision=10,scale=0)
//	private Integer setupTimeFactor;
//	
//	// 使用时间百分数
//	@Column(name="USAGE_PERCENT" ,precision=25,scale=6)
//	private Float usagePercent;
//	
//	// 安装时间百分数
//	@Column(name="USE_FOR_SETUP", precision=2,scale=0)
//	private Integer useForSetup;
//	
//	// 资源组号
//	@Column(name="RESOURCE_GROUP", length=50)
//	private String resourceGroup;
//	
//	// 消耗寿命
//	@Column(name="CONSUME_LIFE" ,precision=20,scale=0)
//	private Integer consumeLife;
//
//	// ？？未知
//	private String tcode;
	

	public OperationResource()
	{
	}
	
	public OperationResource(String operationUid, String resourceUid,
			Integer resourceNum)
	{
		super();
		this.operationUid = operationUid;
		this.resourceUid = resourceUid;
		this.resourceReqQty = resourceNum;
	}
	
	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.uuid;
	}

	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.uuid = uuid;
	}

	public String getOperationUid() {
		return operationUid;
	}

	public void setOperationUid(String operationUid) {
		this.operationUid = operationUid;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public String getResourceUid() {
		return resourceUid;
	}

	public void setResourceUid(String resourceUid) {
		this.resourceUid = resourceUid;
	}

	public String getDrawingId() {
		return drawingId;
	}

	public void setDrawingId(String drawingId) {
		this.drawingId = drawingId;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public String getResourceDescription() {
		return resourceDescription;
	}

	public void setResourceDescription(String resourceDescription) {
		this.resourceDescription = resourceDescription;
	}

	public String getResourceType() {
		return resourceType;
	}

	public void setResourceType(String resourceType) {
		this.resourceType = resourceType;
	}

	public String getResourceTypeDesc() {
		return resourceTypeDesc;
	}

	public void setResourceTypeDesc(String resourceTypeDesc) {
		this.resourceTypeDesc = resourceTypeDesc;
	}

	public Integer getResourceReqQty() {
		return resourceReqQty;
	}

	public void setResourceReqQty(Integer resourceReqQty) {
		this.resourceReqQty = resourceReqQty;
	}

	public String getUseType() {
		return useType;
	}

	public void setUseType(String useType) {
		this.useType = useType;
	}

	public String getRoutingUid() {
		return routingUid;
	}

	public void setRoutingUid(String routingUid) {
		this.routingUid = routingUid;
	}

	public Routing getRouting() {
		return routing;
	}

	public void setRouting(Routing routing) {
		this.routing = routing;
	}

	public Boolean getIsEffective() {
		return isEffective;
	}

	public void setIsEffective(Boolean isEffective) {
		this.isEffective = isEffective;
	}

	public Cutter getCutter() {
		return cutter;
	}

	public void setCutter(Cutter cutter) {
		this.cutter = cutter;
	}

	public Clamp getClamp() {
		return clamp;
	}

	public void setClamp(Clamp clamp) {
		this.clamp = clamp;
	}

	public Measurer getMeasurer() {
		return measurer;
	}

	public void setMeasurer(Measurer measurer) {
		this.measurer = measurer;
	}

	public Boolean isCutter()
	{
		if (this.getResourceType() != null
				&& this.getResourceType().indexOf("0601") >= 0)
		{
			return true;
		}
		return false;
	}

	public Boolean isClamp()
	{
		if (this.getResourceType() != null
				&& this.getResourceType().indexOf("0604") >= 0)
		{
			return true;
		}
		return false;
	}
	
	public Part getResource() {
		return resource;
	}

	public void setResource(Part resource) {
		this.resource = resource;
	}

	public Boolean isMeasurer()
	{
		if (this.getResourceType() != null
				&& this.getResourceType().indexOf("0603") >= 0)
		{
			return true;
		}
		return false;
	}
	
	public boolean equals(Object other)
	{
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof OperationResource))
			return false;
		OperationResource castOther = (OperationResource) other;

		return ((this.getOperationUid() == castOther.getOperationUid()) || (this
				.getOperationUid() != null
				&& castOther.getOperationUid() != null && this
				.getOperationUid().equals(castOther.getOperationUid())))
				&& ((this.getResourceUid() == castOther.getResourceUid()) || (this
						.getResourceUid() != null
						&& castOther.getResourceUid() != null && this
						.getResourceUid().equals(castOther.getResourceUid())));
	}

	public int hashCode()
	{
		int result = 17;

		result = 37
				* result
				+ (getOperationUid() == null ? 0 : this.getOperationUid()
						.hashCode());
		result = 37
				* result
				+ (getResourceUid() == null ? 0 : this.getResourceUid()
						.hashCode());
		return result;
	}




}